package com.cy.controller;

import com.cy.entity.SysCode;
import com.cy.service.SysCodeService;
import com.cy.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 陈熠
 * 2017/7/19.
 */
@RestController
@RequestMapping("/getData")
public class GetDataController {
    @Autowired
    private SysCodeService sysCodeService;

    /**
     * @param
     * @author chenyi
     * @Description 通过枚举获取数据列表
     * @date date 2017-7-20
     */
    @ResponseBody
    @RequestMapping("/getEnum")
    public R getEnum(@RequestParam Map<String, Object> params) throws Exception {
        List<EnumBean> values = new ArrayList<>();
        String enumName = (String) params.get("enumName");
        if (enumName != null && !"".equals(enumName)) {
            Class clzz = null;
            try {
                clzz = Class.forName(Constant.PACKAGE_NAME + "." + enumName);
                Method method = clzz.getMethod("values");
                EnumMessage inter[] = (EnumMessage[]) method.invoke(new Object[]{}, new Object[]{});
                for (EnumMessage enumMessage : inter) {
                    EnumBean e = new EnumBean();
                    e.setCode(enumMessage.getCode());
                    e.setValue(enumMessage.getValue());
                    values.add(e);
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return R.ok().put("data", values);
    }

    /**
     * @param
     * @author chenyi
     * @Description 通过表码获取数据列表
     * @date date 2017-7-20
     */
    @ResponseBody
    @RequestMapping("/getCodeValues")
    public R getCodeValues(@RequestParam Map<String, Object> params) {
        List<SysCode> sysCodeList = null;
        if (params.get("codeName") != null && !"".equals(params.get("codeName"))) {
            sysCodeList = sysCodeService.getCodeValues(params);
        }
        return R.ok().put("data", sysCodeList);
    }
//    @ResponseBody
//    @RequestMapping("/getSexJson")
//    public R getSexJson(@RequestParam Map<String, Object> params) {
//        List<SysCode> sysCodeList = null;
//        if (params.get("codeName") != null && !"".equals(params.get("codeName"))) {
//            sysCodeList = sysCodeService.getCodeValues(params);
//        }
//        return R.ok().put("data", sysCodeList);
//    }

    /**
     * 上传多个文件
     */
    @ResponseBody
    @RequestMapping("/uploadFiles")
    public R uploadFiles(@RequestParam("uploadFiles") MultipartFile[] files) throws Exception {
        List<String> urls=new ArrayList<>();
        for (MultipartFile file : files) {
            if (file.isEmpty()) {
                throw new RRException("上传文件不能为空");
            }
            String fileName = file.getOriginalFilename();
            String _extName = fileName.substring(fileName.indexOf("."), fileName.length());//获取扩展名

            if (file.getSize() > 1 * 1024 * 1024) {
                throw new RRException("图片不能大于1M");
            }
            //上传文件
            String url = UploadUtil.uploadImage(file.getOriginalFilename(),file.getInputStream());
            //String url = "/statics/img/timg.jpg";
            urls.add(url);
        }
        return R.ok().put("urls", urls);
    }

    /**
     * 上传文件
     */
    @ResponseBody
    @RequestMapping("/uploadFile")
    public R uploadFile(@RequestParam("uploadFile") MultipartFile[] file) throws Exception {
        if (file[0].isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        String fileName = file[0].getOriginalFilename();
        String _extName = fileName.substring(fileName.indexOf("."), fileName.length());//获取扩展名

        if (file[0].getSize() > 1 * 1024 * 1024) {
            throw new RRException("图片不能大于1M");
        }
        //上传文件
        String url = UploadUtil.uploadImage(file[0].getOriginalFilename(),file[0].getInputStream());
        //String url = "/statics/img/timg.jpg";
        return R.ok().put("url", url);
    }

}
